﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using 约苗秒杀.Models;

namespace 约苗秒杀.Service
{
    /**
 * Created on 2020-07-22.
 *
 * @author wangxiaodong
 */
    public class SecKillService
    {

        private HttpService httpService;
        public Form1 form;

        //private ExecutorService service = Executors.newFixedThreadPool(200);

        public SecKillService(Form1 form)
        {
            httpService = new HttpService(form);
            this.form = form;
        }

        public void GetTask(long startDate, int vaccineId, int threanId)
        {
            var count = 0;
            while (true)
            {
                form.SendMeg($"线程ID:{threanId}--正在抢购");
                //1.直接秒杀、获取秒杀资格
                var result = httpService.secKill(vaccineId.ToString(), "1", Config.memberId.ToString(), Config.idCard);
                if ((long)(DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0)).TotalMilliseconds > startDate + 1000 * 60 * 2)
                {
                    return;
                }
                Thread.Sleep(500);
            }
        }
        public static long ConvertDateTimeToLong(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = dt.Subtract(dtStart);
            long timeStamp = toNow.Ticks;
            timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
            return timeStamp;
        }

        /**
         * 多线程秒杀开启
         */
        public void startSecKill(int vaccineId, string startDateStr)
        {
            Task.Run(() =>
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;
                if (DateTime.Now > DateTime.Parse(startDateStr))
                {
                    form.SendMeg($"选中的秒杀已过期 秒杀线程{threadId}中止");
                    return;
                }
                long startDate = ConvertDateTimeToLong(DateTime.Parse(startDateStr));
                startDate = startDate - 5000;
                var bool1 = false;
                var bool2 = false;
                var bool3 = false;
                var bool4 = false;
                var bool5 = false;
                List<Task> tasks = new List<Task>();
                while (true)
                {
                    long now = (long)(DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0)).TotalMilliseconds;
                    if (now + 2000 < startDate)
                    {
                        form.SendMeg($"线程ID：{threadId} --还未到开始时间，等待中......");
                        Thread.Sleep(1);
                    }
                    //1000-1500毫秒之间
                    if (now + 3000 >= startDate && now + 2000 < startDate && !bool1)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前1000~1500毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool1 = true;
                    }
                    //1000-1500毫秒之间
                    if (now + 2000 >= startDate && now + 1500 < startDate && !bool1)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前1000~1500毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool1 = true;
                    }
                    //1000-1500毫秒之间
                    if (now + 1500 >= startDate && now + 1000 < startDate && !bool1)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前1000~1500毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool1 = true;
                    }
                    //1500-2000毫秒之间
                    if (now + 1000 >= startDate && now + 500 < startDate && !bool2)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前500~1000毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool2 = true;
                    }
                    //1500-2000毫秒之间
                    if (now + 500 >= startDate && now + 200 < startDate && !bool3)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前200~500毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool3 = true;
                    }
                    //1500-2000毫秒之间
                    if (now + 200 >= startDate && now < startDate && !bool4)
                    {
                        form.SendMeg($"线程ID：{threadId} --提前0~200毫秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool4 = true;
                    }
                    //1500-2000毫秒之间
                    if (now >= startDate && now - 1000 < startDate && !bool5)
                    {
                        form.SendMeg($"线程ID：{threadId} --正点秒杀开始执行");
                        for (int i = 0; i < 2; i++)
                        {
                            tasks.Add(Task.Run(() => { GetTask(startDate, vaccineId, threadId); }));
                        }
                        bool5 = true;
                    }
                    if (bool1 && bool2 && bool3 && bool4 && bool5) break;
                    int sellp = 1000;
                    long sjc = startDate - now;
                    if (sjc < 10000) sellp = 500;
                    if (sjc < 5000) sellp = 100;
                    if (sjc < 3000) sellp = 10;
                    Thread.Sleep(sellp);
                    form.SendMeg($"线程ID：{threadId} --正在执行秒杀-距离秒杀开始还有{(startDate - now) / 1000}秒");
                    if (startDate - now < 0) break;
                }

                Task.WaitAll(tasks.ToArray());
            });
        }
        public List<VaccineList> getVaccines()
        {
            return httpService.getVaccineList();
        }
        /**
         *  将时间字符串转换为时间戳
         * @param dateStr yyyy-mm-dd格式
         * @return
         */
        public long convertDateToInt(string dateStr)
        {
            return (long)(DateTime.Parse(dateStr) - new DateTime(1970, 1, 1, 8, 0, 0)).TotalMilliseconds;
        }


    }

}
